import lazyload from './lazyload';

const FD_PICTURE_FLAG = 'data-feidao-src';

/**
 * 延迟加载图片
 * @param node
 */
export default function lazyload_picture(node: Element) {
	const img_list = Array.from(node.querySelectorAll(`img[${FD_PICTURE_FLAG}]`)) as HTMLImageElement[];
	img_list.forEach((img) => {
		lazyload(img).then(() => {
			const pic_src = img.getAttribute(FD_PICTURE_FLAG);
			if (!pic_src) {
				return;
			}
			const image = new Image();
			image.onload = () => {
				img.src = pic_src;
				img.removeAttribute(FD_PICTURE_FLAG);
			};
			image.src = pic_src;
		});
	});
}
